package com.rizzo.web.util.domain;

/**
 * Enum to represent several contenttypes used for streaming via HTTP.
 *
 * @author Jan Lievens
 */
public enum ContentTypeEnum {

    FILETYPE_ZIP("application/zip"),
    FILETYPE_TXT("text/plain"),
    FILETYPE_PDF("application/pdf"),
    FILETYPE_XML("text/xml"),
    FILETYPE_DOC("application/msword"),
    FILETYPE_ODT("application/vnd.oasis.opendocument.text"),
    FILETYPE_XLS("application/vnd.ms-excel"),
    FILETYPE_PPT("application/vnd.ms-powerpoint"),
    FILETYPE_PPS("application/vnd.ms-powerpoint"),
    FILETYPE_POT("application/vnd.ms-powerpoint"),
    FILETYPE_DEFAULT("application/octet-stream"),
    FILETYPE_CSV("text/csv");

    private String contentType;

    /**
     * Default constructor.
     *
     * @param contentType
     */
    ContentTypeEnum(String contentType) {
        this.contentType = contentType;
    }

    public String getContentType() {
        return contentType;
    }

    /**
     * Resolves the ContentType of a given filename presented in a String.
     *
     * @param filename The filename of which the {@link com.rizzo.web.util.domain.ContentTypeEnum} must be extracted.
     * @return A corresponding {@link com.rizzo.web.util.domain.ContentTypeEnum}.
     */
    public static ContentTypeEnum resolveContentType(String filename) {
        if (filename == null)
            return FILETYPE_DEFAULT;

        filename = filename.toLowerCase();
        String[] list = filename.split("[.]");
        if (list.length > 1)
            filename = list[list.length - 1];
        if ("zip".equals(filename))
            return FILETYPE_ZIP;
        if ("txt".equals(filename))
            return FILETYPE_TXT;
        if ("pdf".equals(filename))
            return FILETYPE_PDF;
        if ("xml".equals(filename))
            return FILETYPE_XML;
        if ("doc".equals(filename))
            return FILETYPE_DOC;
        if ("odt".equals(filename))
            return FILETYPE_ODT;
        if ("xls".equals(filename))
            return FILETYPE_XLS;
        if ("ppt".equals(filename))
            return FILETYPE_PPT;
        if ("pps".equals(filename))
            return FILETYPE_PPS;
        if ("pot".equals(filename))
            return FILETYPE_POT;
        if ("csv".equals(filename))
            return FILETYPE_CSV;
        return FILETYPE_DEFAULT;
    }

    /**
     * returns everything after the last '.'
     *
     * @param filename The name of the complete filenam with extension.
     * @return The available extension, if no extension available it will return an empty String.
     */
    public static String fetchExtension(String filename) {
        String extension = "";
        if (filename != null) {
            filename = filename.toLowerCase();
            String[] list = filename.split("[.]");
            if (list.length > 1)
                extension = list[list.length - 1];
        }
        return extension;
    }
}